package com.infore.exchange.main.service.impl;


import cn.hutool.core.date.DateUtil;
import com.infore.common.fw.DateUnit;
import com.infore.common.model.dto.ChartsVo;
import com.infore.common.utils.DateUtils;
import com.infore.common.utils.NumberUtil;
import com.infore.exchange.main.dao.StatisticalMapper;
import com.infore.exchange.main.service.StatisticalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据统计 服务实现类
 * </p>
 *
 * @author lijh
 * @since 2023-02-07
 */
@Service
public class StatisticalServiceImpl implements StatisticalService {


    @Value("${spring.datasource.druid.dataExchange.driver-alias}")
    private String driverAlias;


    @Autowired
    private StatisticalMapper statisticalMapper;

    @Override
    public ChartsVo queryDataCollectionStatisticsOfLastDay(int day) {
        String startDateStr = DateUtil.format(DateUtil.offsetDay(new Date(), (-day+1)),DateUtils.FORMAT_05) ;
        String endDateStr=DateUtil.format(new Date(),DateUtils.FORMAT_05);
        List<String> xList = DateUtils.getDateStrBetween(startDateStr, endDateStr, DateUnit.DAY, DateUtils.FORMAT_05);

        List<Map<String, Object>> list = statisticalMapper.queryDataCollectionByDays(day-1);
        Map<Object, List<Map<String, Object>>> map = list.stream().collect(Collectors.groupingBy(g -> g.get("type")));

        List<Map<String, Object>> series = new ArrayList<>();
        List<String> legend = new ArrayList<>();
        for (Map.Entry<Object,List<Map<String, Object>>> entry:map.entrySet()) {
            Map<String, Object> serie = new HashMap<>();
            List<Object> yAxis = new ArrayList<>();
            String key = entry.getKey().toString();
            legend.add(key);
            List<Map<String, Object>> value = entry.getValue();
            for (String xaixs : xList) {
                int dataValue = 0;
                for (Map<String, Object> row : value) {
                    String dateStr = row.get("dateStr").toString();
                    if (xaixs.equals(dateStr)) {
                        dataValue =Integer.parseInt( row.get("rowCount").toString());
                        break;
                    }
                }
                yAxis.add(dataValue);

            }
            serie.put("name", key);
            serie.put("unit", "次");
            serie.put("data", yAxis);
            serie.put("type", "line");
            series.add(serie);

        }
        ChartsVo vo = new ChartsVo();
        vo.setLegend(legend);
        vo.setXAxis(xList);
        vo.setSeries(series);
        return vo;
    }

    @Override
    public Map<String,Object> queryDataCollectionRatio() {

        Map<String,Object> returnMap = new HashMap<>();
        List<Map<String, Object>> list = statisticalMapper.queryDataCollectionRatio(driverAlias);
        Map<String, Object> other = new HashMap<>();
        other.put("type","其他");
        other.put("rowCount",0);
        list.add(other);

        long total = 0;
        for (Map<String, Object> row:list ) {
            total=total+Long.valueOf( row.get("rowCount").toString());
        }
        returnMap.put("total",total);
        Map<String, Object> valUnit = NumberUtil.getValUnit(total);
        returnMap.put("totalCN",valUnit.get("val"));
        returnMap.put("unit",valUnit.get("unit")==null? "条":valUnit.get("unit").toString()+"条");

        returnMap.put("data",list);
        return returnMap;
    }
    @Override
    public boolean DayResourceStatistics(){
        int deletecount = statisticalMapper.DayResourceDelete();
        System.out.println("deletecount="+deletecount);
        if(deletecount>0){
            int insertcount = statisticalMapper.DayResourceStatistics();
            System.out.println("insertcount="+insertcount);
        }
        return true;
    }
}
